home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / opbonus.arc / FBROWSE.ARC / OOPSEMA.PAS < prev   
Pascal/Delphi Source File  |  1991-03-20  |  6KB  |  210 lines

  1. {
  2.   OOPSEMA 1.0 - NetWare semaphore related objects
  3.  
  4.   by Richard S. Sadowsky
  5.   Released to the public domain
  6.  
  7.   Please address questions and comments about this unit to ALL in section 6 of
  8.   the PCVENB forum on Compuserve.
  9. }
  10.  
  11. {$I-,V-,S-,R-}
  12. unit OopSema;
  13.  
  14. interface
  15.  
  16. uses NetSema;
  17.  
  18. const
  19.   MaxSemaphores = 100; {maximum number of semaphores managed by FilerSemaphore}
  20.  
  21. type
  22.   SimpleSemaphorePtr = ^SimpleSemaphore;
  23.   {The following object type simply encapsulates all the NetWare semaphore
  24.    functions into an object}
  25.   SimpleSemaphore =
  26.     object
  27.       ssName       : SemaphoreName;
  28.       ssHandle     : LongInt;
  29.       constructor Init(Name : SemaphoreName;
  30.                        InitValue : Byte;
  31.                        var OpenCount : Byte);
  32.         {-Open a semaphore}
  33.       destructor Done; Virtual;
  34.         {-Close a semaphore}
  35.       function Examine(var Value : ShortInt; var OpenCount : Byte) : Boolean;
  36.         {-Examine Value and OpenCount for this semaphore}
  37.       function Signal(var Overflow : Boolean) : Boolean;
  38.         {-Increment Value of semaphore}
  39.       function WaitOn(TimeOutValue : Word;
  40.                       var TimeOut : Boolean) : Boolean;
  41.         {-Decrement a semaphore}
  42.       function GetName : SemaphoreName;
  43.         {-Return the name of this semaphore}
  44.     end;
  45.  
  46.   SemaphoreRec   = record
  47.                      Sema : SimpleSemaphorePtr;
  48.                      Valu : ShortInt;
  49.                      Cnt  : Byte;
  50.                    end;
  51.   SemaphoreList  = Array[1..MaxSemaphores] of SemaphoreRec;
  52.  
  53.   {This is a higher level semaphore object used to provide synchronization
  54.    between workstations. See TurboPower's FBDEMO for an example usage of this
  55.    object type.}
  56.   FilerSemaphore =
  57.     object
  58.       fsNrOfKeys   : Integer;
  59.       fsSemaphores : ^SemaphoreList;
  60.       constructor Init(Name : String; NrOfKeys : Integer);
  61.         {-create the semaphores}
  62.       destructor Done; Virtual;
  63.         {-destroy the semaphores}
  64.       procedure IndicateDirty(KeyNr : Integer);
  65.         {-Indicate to others that a file has been modified}
  66.       function IsDirty(KeyNr : Integer) : Boolean;
  67.         {-see if the file has been modified}
  68.       function NumberOpen(KeyNr : Integer) : Byte;
  69.         {-Return the number of stations currently using the semaphore}
  70.     end;
  71.  
  72. implementation
  73.  
  74.   constructor SimpleSemaphore.Init(Name : SemaphoreName;
  75.                                    InitValue : Byte;
  76.                                    var OpenCount : Byte);
  77.     {-Open a semaphore}
  78.   begin
  79.     if OpenSemaphore(Name, InitValue, OpenCount, ssHandle) then
  80.       ssName := Name
  81.     else
  82.       Fail;
  83.   end;
  84.  
  85.   destructor SimpleSemaphore.Done;
  86.     {-Close a semaphore}
  87.   begin
  88.     if CloseSemaphore(ssHandle) then ;
  89.   end;
  90.  
  91.   function SimpleSemaphore.Examine(var Value : ShortInt; var OpenCount : Byte) : Boolean;
  92.     {-Examine Value and OpenCount for this semaphore}
  93.   begin
  94.     Examine := ExamineSemaphore(ssHandle, Value, OpenCount);
  95.   end;
  96.  
  97.   function SimpleSemaphore.Signal(var Overflow : Boolean) : Boolean;
  98.     {-Increment Value of semaphore}
  99.   begin
  100.     Signal := SignalSemaphore(ssHandle, Overflow);
  101.   end;
  102.  
  103.   function SimpleSemaphore.WaitOn(TimeOutValue : Word;
  104.                            var TimeOut : Boolean) : Boolean;
  105.     {-Decrement a semaphore}
  106.   begin
  107.     WaitOn := WaitOnSemaphore(ssHandle, TimeOutValue, TimeOut);
  108.   end;
  109.  
  110.   function SimpleSemaphore.GetName : SemaphoreName;
  111.   begin
  112.     GetName := ssName;
  113.   end;
  114.  
  115.   function IntToStr(I : Integer) : String;
  116.   var
  117.     S : String;
  118.   begin
  119.     Str(I, S);
  120.     IntToStr := S;
  121.   end;
  122.  
  123.   constructor FilerSemaphore.Init(Name : String; NrOfKeys : Integer);
  124.     {-create the semaphore}
  125.   var
  126.     I, II : Integer;
  127.     Size  : Word;
  128.   begin
  129.     if (NrOfKeys = 0) or (NrOfKeys > MaxSemaphores) then
  130.       Fail;
  131.     fsNrOfKeys := NrOfKeys;
  132.     Size := NrOfKeys * SizeOf(SemaphoreRec);
  133.     if MaxAvail < LongInt(Size) then
  134.       Fail;
  135.     GetMem(fsSemaphores, Size);
  136.     for I := 1 to NrOfKeys do begin
  137.       with fsSemaphores^[I] do begin
  138.         New(Sema, Init('fs__'+Name+IntToStr(I), 0 , Cnt));
  139.         if Sema = Nil then begin
  140.           for II := Pred(I) downto 1 do
  141.             Dispose(fsSemaphores^[II].Sema, Done);
  142.           FreeMem(fsSemaphores, Size);
  143.           Fail;
  144.         end
  145.         else
  146.           if Sema^.Examine(Valu, Cnt) then ;
  147.       end;
  148.     end;
  149.   end;
  150.  
  151.   destructor FilerSemaphore.Done;
  152.   var
  153.     I : Integer;
  154.   begin
  155.     for I := 1 to fsNrOfKeys do
  156.       Dispose(fsSemaphores^[I].Sema, Done);
  157.     FreeMem(fsSemaphores, fsNrOfKeys * SizeOf(SemaphoreRec));
  158.   end;
  159.  
  160.   procedure FilerSemaphore.IndicateDirty(KeyNr : Integer);
  161.     {-Indicate to others that a file has been modified}
  162.   var
  163.     Overflow : Boolean;
  164.     I  : Byte;
  165.  
  166.   begin
  167.     if (KeyNr < 1) or (KeyNr > fsNrOfKeys) then
  168.       Exit;
  169.     with fsSemaphores^[KeyNr] do
  170.       if Sema^.Signal(Overflow) then begin
  171.         if Overflow then
  172.           {if overflow, then reset downto 1}
  173.           for I := 127 downto 1 do
  174.             if Sema^.WaitOn(0, Overflow) then ;
  175.         if not Sema^.Examine(Valu, Cnt) then ;
  176.       end;
  177.   end;
  178.  
  179.   function FilerSemaphore.IsDirty(KeyNr : Integer) : Boolean;
  180.     {-see if the file has been modified}
  181.   var
  182.     NewValue : ShortInt;
  183.   begin
  184.     IsDirty := False;
  185.     if (KeyNr < 1) or (KeyNr > fsNrOfKeys) then
  186.       Exit;
  187.     with fsSemaphores^[KeyNr] do
  188.       if Sema^.Examine(NewValue, Cnt) then begin
  189.         if NewValue <> Valu then begin
  190.           IsDirty := True;
  191.           Valu := NewValue;
  192.         end;
  193.       end;
  194.   end;
  195.  
  196.   function FilerSemaphore.NumberOpen(KeyNr : Integer) : Byte;
  197.   var
  198.     C : Byte;
  199.     V : ShortInt;
  200.   begin
  201.     NumberOpen := 0;
  202.     if (KeyNr < 1) or (KeyNr > fsNrOfKeys) then
  203.       Exit;
  204.     with fsSemaphores^[KeyNr] do
  205.       if Sema^.Examine(V, C) then
  206.         NumberOpen := C;
  207.   end;
  208.  
  209. end.
  210.